package com.jiyun.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.common.R;
import com.jiyun.entity.Task;
import com.jiyun.entity.TaskDTO;
import com.jiyun.service.ITaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-03-10
 */
@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    ITaskService taskService;

    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public void addTask(@RequestBody Task task,@RequestParam List<Integer> ids){
        taskService.insert(task,ids);
    }

    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public void updateTask(@RequestBody Task task,@RequestParam List<Integer> ids){
        taskService.updateTask(task,ids);
    }

    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public void deleteOne(@RequestParam Integer id){
        taskService.removeById(id);
    }

    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids){
        taskService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public Task findById(@RequestParam Integer id){
        return taskService.getById(id);
    }

    @RequestMapping("/findZhong")
    public List<Integer> findZhong(Integer tid){
        return taskService.findZhong(tid);
    }

    /**
     * 查询所有接口
     */
    @RequestMapping("/findAll")
    public List<Task> findAll(@RequestParam Integer id){
        return taskService.findAllOutId(id);
    }

    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */
    @RequestMapping("/findPage")
    public Page<TaskDTO> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody Map<String,String> map){
        Page<Task> taskPage = new Page<>(pageNum, pageSize);
        return taskService.taskPage(taskPage,map);
    }
    @RequestMapping("/find2Page")
    public Page<TaskDTO> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        Page<Task> taskPage = new Page<>(pageNum, pageSize);
        return taskService.task2Page(taskPage);
    }

    @RequestMapping("verify")
    public R verify(Integer id,String status){
        taskService.verify(id,status);
        return R.success("审核成功或失败！");
    }

    @RequestMapping("treeData")
    public List<TaskDTO> treeData(){
        List<TaskDTO> list = taskService.findAll();

        return getListByParentId(list,0);
    }

    private List<TaskDTO> getListByParentId(List<TaskDTO> list, int pid) {
        // 利用递归，来处理
        List<TaskDTO> treeData = new ArrayList<>();
        for (TaskDTO task : list) {
            if (task.getPid()==pid) {

                List<TaskDTO> children = getListByParentId(list, task.getId());
                task.setChildren(children);
                treeData.add(task);
            }
        }
        return treeData;
    }

    @RequestMapping("findTaskChildren")
    public List<Integer> findTaskChildren(@RequestParam Integer pid){
        return taskService.findTaskChildren(pid);
    }

    @RequestMapping("addChildrenTask")
    public void addChildrenTask(@RequestParam Integer pid,@RequestParam List<Integer> ids){
        taskService.addChildrenTask(pid,ids);
    }
}

